﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonLibrary.Web.Core
{
    public class EntityService<T> where T : class
    {
        /// <summary>
        /// Repository used by the service.
        /// </summary>
        public EntityRepository<T> Repository { get; set; }

        
        /// <summary>
        /// Creates an item in the repository.
        /// </summary>
        /// <param name="item"></param>
        public virtual void Create(T item)
        {
            this.ApplyAuditInfo(item);
            this.Repository.Create(item);
        }


        /// <summary>
        /// Gets an item from the repository by it's id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual T Get(int id)
        {
            return this.Repository.Get(id);
        }


        /// <summary>
        /// Deletes an item from the repository
        /// </summary>
        /// <param name="item"></param>
        public virtual void Delete(T item)
        {
            var id = EntityHelper.GetId(item);
            this.Delete(id);
        }


        /// <summary>
        /// Deletes an item from the repository
        /// </summary>
        /// <param name="item"></param>
        public virtual void Delete(int id)
        {
            this.Repository.Delete(id);
        }


        /// <summary>
        /// Updates the item from the repository
        /// </summary>
        /// <param name="item"></param>
        public virtual void Update(T item)
        {
            this.ApplyAuditInfo(item);
            this.Repository.Update(item);
        }


        /// <summary>
        /// Gets all the items from the repository.
        /// </summary>
        /// <returns></returns>
        public virtual List<T> GetAll()
        {
            return this.Repository.GetAll();
        }


        /// <summary>
        /// Apply audit info on the object.
        /// </summary>
        /// <param name="item"></param>
        public void ApplyAuditInfo(object item)
        {
            if( !(item is IEntity))
                return;

            var entity = item as IEntity;
            if(entity.Id <= 0)
            {
                entity.CreateUser = this.GetCurrentUser();
                entity.CreateDate = DateTime.Now;
            }

            entity.UpdateUser = this.GetCurrentUser();
            entity.UpdateDate = DateTime.Now;
        }


        /// <summary>
        /// Get the current user name.
        /// </summary>
        /// <returns></returns>
        public string GetCurrentUser()
        {
            return "testuser1";
        }


        /// <summary>
        /// Get the current user id.
        /// </summary>
        /// <returns></returns>
        public int GetCurrentUserId()
        {
            return 1;
        }
    }
}
